﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using WebApi.Service;
using WebApi.Service.Dto;
using WebApi.Service.Other;
using WebApi.Web.Models;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi.Web.Controllers
{
    /// <summary>
    /// 商品相关的controller
    /// </summary>
    [Route("api/[controller]")]
    public class ProductController : Controller
    {
        private readonly IProductService productService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productService"></param>
        public ProductController(IProductService productService)
        {
            this.productService = productService;
        }
        /// <summary>
        /// 创建商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseResult<bool> CreateProduct(CreateProductModel product)
        {
            return productService.CreateProduct(new CreateProductDto
            {
                ItemNumber = product.ItemNumber,
                Name = product.Name,
                Price = product.Price,
                Description = product.Description,
                TagsId = product.TagsId
            });
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id">删除商品的id</param>
        /// <returns></returns>
        [HttpDelete]
        public ResponseResult<bool> DeleteProduct(List<long> productsId)
        {
            return productService.DeleteProduct(productsId);
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="product">修改相关名称、描述、标签的值</param>
        /// <returns></returns>
        [HttpPut]
        public ResponseResult<bool> UpdateProduct(UpdateProductModel product)
        {

            return productService.UpdateProduct(new UpdateProductDto
            {
                Id = product.Id,
                Name = product.Name,
                Price = product.Price,
                Description = product.Description,
                TagsId = product.TagsId
            });
        }
        /// <summary>
        /// 根据筛选条件返回结果
        /// </summary>
        /// <param name="tagId">所选的标签，-1为全部</param>
        /// <param name="state">上架状态，-1为全部</param>
        /// <param name="search">搜索框的内容</param>
        /// <param name="index">分页的页码</param>
        /// <returns></returns>
        [HttpGet]
        public ResponseResult<List<GetProductModel>> GetProducts(int tagId, int state, string search,int index)
        {
            var res = productService.GetProducts(tagId, state, search,index);
            var data=res.Data.Select(o => new GetProductModel
            {
                ItemNumber = o.ItemNumber,
                Name = o.Name,
                Price = o.Price,
                State = o.State,
                CreateAt = o.CreateAt,
                UpdateAt = o.UpdateAt
            }).ToList();
            return new ResponseResult<List<GetProductModel>>()
            {
                State = res.State,
                Data = data,
                Message = res.Message
            };
        }
        /// <summary>
        /// 上架商品
        /// </summary>
        /// <param name="productsId">需要上架的商品的id</param>
        /// <returns></returns>
        [HttpPost("puton")]
        public ResponseResult<bool> PutOnProducts(List<long> productsId)
        {
            return productService.PutOnProducts(productsId);
        }
        /// <summary>
        /// 下架商品
        /// </summary>
        /// <param name="productsId">需要下架的商品id</param>
        /// <returns></returns>
        [HttpPost("pulloff")]
        public ResponseResult<bool> PullOffProducts(List<long>productsId)
        {
            return productService.PullOffProducts(productsId);
        }
        /// <summary>
        /// 获取商品的详细信息
        /// </summary>
        /// <param name="id">需要获取的商品的id</param>
        /// <returns></returns>
        [HttpGet("detail")]
        public ResponseResult<ProductDetailModel> GetProductDetail(long id)
        {
            var res = productService.GetProductDetail(id);
            var o = res.Data;
            var data = new ProductDetailModel
            {
                Id = o.Id,
                ItemNumber = o.ItemNumber,
                Name = o.Name,
                Price = o.Price,
                State = o.State,
                CreateAt = o.CreateAt,
                UpdateAt = o.UpdateAt
            };
            return new ResponseResult<ProductDetailModel>()
            {
                State = res.State,
                Data = data,
                Message = res.Message
            };
        }
    }
}
